package com.lg.cms.controller.news;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lg.cms.entity.Article;
import com.lg.cms.service.*;
import com.lg.cms.utils.RedisUtil;
import com.lg.cms.vo.news.ArticleVO;
import com.lg.cms.vo.news.CommentVO;
import com.lg.cms.vo.news.ImageVO;
import com.lg.cms.vo.news.VideoVO;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Description: 前台展示分页数据视图
 * @Author: wuyuhang
 * @create: 2020-10-13 18:22
 */
@RestController
public class NewsPageController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private CommentService commentService;
    @Resource
    RedisUtil redisUtil;
    //无
    /**
     * 获取文章列表, page对象
     * @param pageNum
     * @param categoryId
     * @return
     */
    @GetMapping("/get_article_page")
    public Page<ArticleVO> getArticlePage(Integer pageNum, Integer categoryId){
        return articleService.getPageByCategoryId(categoryId, pageNum);
    }
    /**
     * 获取推荐列表, page对象
     * @param pageNum
     * @return
     */
    @GetMapping("/get_recommended_page")
    public Page<ArticleVO> getRecommendedPage(Integer pageNum){
        return articleService.getPageByRecommended(pageNum);
    }
    /**
     * 获取图片列表, page对象
     * @param pageNum
     * @return
     */
    @GetMapping("/get_image_page")
    public Page<ImageVO> getImagePage(Integer pageNum, String name){
        QueryWrapper wrapper = new QueryWrapper();
        if(name != null)
            wrapper.like("title", name);
        return articleService.getPageByImage(pageNum, wrapper);
    }
    /**
     * 获取视频列表, page对象
     * @param pageNum
     * @return
     */
    @GetMapping("/get_video_page")
    public Page<VideoVO> getVideoPage(Integer pageNum, String name){
        QueryWrapper wrapper = new QueryWrapper();
        if(name != null)
            wrapper.like("title", name);
        return articleService.getPageByVideo(pageNum, wrapper);
    }

    /**
     * 获取搜索文章页page对象
     * @param pageNum
     * @param value
     * @param title
     * @return
     */
    @GetMapping("/get_select_article_page")
    public Page<ArticleVO> getSelectArticlePage(Integer pageNum, String value, Boolean title){
        if (pageNum == null) {
            pageNum = 1;
        }
        QueryWrapper<Article> wrapper = new QueryWrapper();
        if (title == null) {
            title = false;
        }
        if (title) {
            wrapper.like("title", value);
            wrapper.isNotNull("title");
        } else {
            wrapper.like("content", value);
            wrapper.isNotNull("markdown_content");
        }
        return articleService.getPageByWrapper(wrapper, pageNum, value);
    }

    /**
     * 获取文章评论page对象
     * @param articleId
     * @param pageNum
     * @return
     */
    @GetMapping("/get_comment_page")
    public Page<CommentVO> getCommentPage(Integer articleId, Integer pageNum){
        return commentService.getPageByArticleId(articleId, pageNum);
    }
    /**
     * 获取某标签文章page对象
     * @param tagsId
     * @param pageNum
     * @return
     */
    @GetMapping("/get_tags_page")
    public Page<ArticleVO> getTagsPage(Integer tagsId, Integer pageNum){
        return articleService.getPageByTagsId(tagsId, pageNum);
    }
}